package api

import (
	"context"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type GoodsApi struct {
	Ctx context.Context
}

func NewGoodsApi() *GoodsApi {
	return &GoodsApi{}
}

// 同步总部定制化商品
func (c *GoodsApi) SyncSupplierGoods() types.JsResp {
	resp := types.JsResp{}
	// 默认传
	body := map[string]interface{}{
		"supplierOrganizeId": 10,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/syncSupplierGoods",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 所有商品大类
func (c *GoodsApi) GetGoodsTotalCategory() types.GoodsTotalCategoryResp {
	resp := types.GoodsTotalCategoryResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/bms/totalCategory/all",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 所有商品分类 先不关联品牌
func (c *GoodsApi) GetGoodsCategory(generalId int64) types.GoodsCategoryResp {
	resp := types.GoodsCategoryResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/bms/category/all",
		QueryParams: map[string]interface{}{
			"generalId": generalId,
		},
		Resp: &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) ListGoodsByDictType(req types.GoodsListByDictTypeReq) types.OrganizeGoodsResp {
	resp := types.OrganizeGoodsResp{}
	body := map[string]interface{}{
		"generalId":          req.GeneralId,
		"categoryId":         req.CategoryId,
		"storeId":            req.StoreId,
		"keyword":            req.KeyWord,
		"ids":                req.Ids,
		"specsList":          req.SpecsList,
		"parentId":           req.ParentId,
		"supplierOrganizeId": req.SupplierOrganizeId,
		"page":               req.Page,
		"pageSize":           req.PageSize,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/byDictType",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询售卖单列表
func (c *GoodsApi) GetGlassesStoreOrderList(req types.GetGlassesStoreOrderListReqForApi) types.OrderWithCustomerForListForApiListResp {
	resp := types.OrderWithCustomerForListForApiListResp{}
	queryParams := map[string]interface{}{
		"page":     req.Page,
		"pageSize": req.PageSize,
	}
	if req.Status != 0 {
		queryParams["status"] = req.Status
	}
	if req.CustomerInfo != "" {
		queryParams["customerInfo"] = req.CustomerInfo
	}
	if req.OrderCode != "" {
		queryParams["orderCode"] = req.OrderCode
	}
	if req.StartDate != 0 {
		queryParams["startDate"] = req.StartDate
	}
	if req.EndDate != 0 {
		queryParams["endDate"] = req.EndDate
	}
	if req.OrganId != 0 {
		queryParams["organId"] = req.OrganId
	}
	if req.CheckListId != 0 {
		queryParams["checkListId"] = req.CheckListId
	}
	if req.CustomerId != 0 {
		queryParams["customerId"] = req.CustomerId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/glassesStore/order/getGlassesStoreOrderList",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询售货单详情
func (c *GoodsApi) GetOrderDetail(orderId int64) types.GetGlassesStoreOrderDetailRespForApiResp {
	resp := types.GetGlassesStoreOrderDetailRespForApiResp{}
	queryParams := make(map[string]any)
	queryParams["id"] = orderId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/glassesStore/order/getOrderDetail",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询商品列表
func (c *GoodsApi) GetGoodsList(req types.GoodsQueryParams) types.GoodsListResp {
	resp := types.GoodsListResp{}
	queryParams := make(map[string]any)
	if req.Spelling != "" {
		queryParams["spelling"] = req.Spelling
	}
	if req.MyCode != "" {
		queryParams["myCode"] = req.MyCode
	}
	if req.GoodsTitle != "" {
		queryParams["goodsTitle"] = req.GoodsTitle
	}
	if req.IsSelling != 0 {
		queryParams["isSelling"] = req.IsSelling
	}
	if req.BrandId != 0 {
		queryParams["brandId"] = req.BrandId
	}
	if req.CategoryId != 0 {
		queryParams["categoryId"] = req.CategoryId
	}
	if req.TypeId != 0 {
		queryParams["typeId"] = req.TypeId
	}
	queryParams["page"] = req.Page
	queryParams["pageSize"] = req.PageSize
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/goodsList",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询品牌列表
func (c *GoodsApi) GetBrandList(req types.DropDownBrandQuery) types.BrandDropDownListResp {
	resp := types.BrandDropDownListResp{}
	queryParams := make(map[string]any)
	if req.CategoryId != 0 {
		queryParams["categoryId"] = req.CategoryId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/brand/all",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// @doc "查询商品详情信息(在用)"
func (c *GoodsApi) GetGoodsInfo(req types.GoodsQueryId) types.GoodsInfoResp {
	resp := types.GoodsInfoResp{}
	queryParams := make(map[string]any)
	if req.ParentId != 0 {
		queryParams["parentId"] = req.ParentId
	}
	queryParams["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/goods",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// goodsType/all
func (c *GoodsApi) GetGoodsTypeList(req types.GoodsTypeQueryId) types.GoodsTypeDropDownListResp {
	resp := types.GoodsTypeDropDownListResp{}
	queryParams := make(map[string]any)
	if req.BrandId != 0 {
		queryParams["brandId"] = req.BrandId
	}
	queryParams["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/goodsType/all",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 修改商品信息
// post /goods/update
func (c *GoodsApi) UpdateGoodsInfo(req types.GoodsAddInfo) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"id":           req.Id,
		"goodsTitle":   req.GoodsTitle,
		"spelling":     req.Spelling,
		"myCode":       req.MyCode,
		"brandId":      req.BrandId,
		"categoryId":   req.CategoryId,
		"generalId":    req.GeneralId,
		"typeId":       req.TypeId,
		"typeName":     req.TypeName,
		"brandName":    req.BrandName,
		"categoryName": req.CategoryName,
		"description":  req.Description,
		"isSelling":    req.IsSelling,
		"goodsImg":     req.GoodsImg,
	}
	if req.SupplierId != 0 {
		body["supplierId"] = req.SupplierId
	}
	if req.SupplierName != "" {
		body["supplierName"] = req.SupplierName
	}
	if req.IsCustomized != 0 {
		body["isCustomized"] = req.IsCustomized
	}
	if len(req.CommonFiles) > 0 {
		body["commonFiles"] = req.CommonFiles
	}
	if len(req.List) > 0 {
		body["list"] = req.List
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/update",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 添加商品信息
// post /goods/add
func (c *GoodsApi) AddGoodsInfo(req types.GoodsAddInfo) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"goodsTitle":   req.GoodsTitle,
		"spelling":     req.Spelling,
		"myCode":       req.MyCode,
		"brandId":      req.BrandId,
		"categoryId":   req.CategoryId,
		"generalId":    req.GeneralId,
		"typeId":       req.TypeId,
		"typeName":     req.TypeName,
		"brandName":    req.BrandName,
		"categoryName": req.CategoryName,
		"description":  req.Description,
		"isSelling":    req.IsSelling,
		"goodsImg":     req.GoodsImg,
	}
	if req.SupplierId != 0 {
		body["supplierId"] = req.SupplierId
	}
	if req.SupplierName != "" {
		body["supplierName"] = req.SupplierName
	}
	if req.IsCustomized != 0 {
		body["isCustomized"] = req.IsCustomized
	}
	if len(req.CommonFiles) > 0 {
		body["commonFiles"] = req.CommonFiles
	}
	if len(req.List) > 0 {
		body["list"] = req.List
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/add",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 下架组织机构产品
// post /goods/delete
func (c *GoodsApi) NotSaleGoods(req types.NotSalingParams) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"id":        req.Id,
		"isSelling": req.IsSelling,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/organize/notSale",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 生成商品编码
func (c *GoodsApi) GenerateGoodsCode() types.OrganizeGoodsCodeResp {
	resp := types.OrganizeGoodsCodeResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/bms/goods/codeGenerate",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// /goodsCategory/specList 获取分类下的规格信息
func (c *GoodsApi) ListGoodsCategorySpecList(req types.GoodsQueryId) types.GoodsSpecsListResp {
	resp := types.GoodsSpecsListResp{}
	queryParams := make(map[string]any)
	if req.Id != 0 {
		queryParams["id"] = req.Id
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/goodsCategory/specList",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询商品规格价格列表
// ListGoodsWithSpecs
func (c *GoodsApi) ListGoodsWithSpecs(req types.GoodsWithSpecsQuery) types.GoodsWithSpecsListResp {
	resp := types.GoodsWithSpecsListResp{}
	body := make(map[string]any)
	if req.GoodsId != 0 {
		body["goodsId"] = req.GoodsId
	}
	if len(req.SpecsList) > 0 {
		body["specsList"] = req.SpecsList
	}
	if req.Page != 0 {
		body["page"] = req.Page
	}
	if req.PageSize != 0 {
		body["pageSize"] = req.PageSize
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/listWithSpecs",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 添加商品规格信息
func (c *GoodsApi) AddGoodsSpec(req types.GoodsSpecAddInfo) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	if req.Id != 0 {
		body["id"] = req.Id
	}
	body["goodsId"] = req.GoodsId
	body["goodsPrice"] = req.GoodsPrice
	body["specsList"] = req.SpecsList
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/specAdd",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 修改规格 specUpdate
func (c *GoodsApi) UpdateGoodsSpec(req types.GoodsSpecAddInfo) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["id"] = req.Id
	body["goodsId"] = req.GoodsId
	body["goodsPrice"] = req.GoodsPrice
	body["specsList"] = req.SpecsList
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/specUpdate",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 删除规格
func (c *GoodsApi) DeleteGoodsSpec(req types.GoodsSpecDeleteParams) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["id"] = req.Id
	body["goodsId"] = req.GoodsId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/goods/specDelete",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// get /goods/specInfo (GoodsQueryId) returns (GoodsSpecAddInfo)
func (c *GoodsApi) GetGoodsSpecInfo(req types.GoodsQueryId) types.GoodsSpecInfoResp {
	resp := types.GoodsSpecInfoResp{}
	queryParams := make(map[string]any)
	queryParams["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/goods/specInfo",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) CreateProviderToOptician(req types.ProviderToOpticianInfo) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["providerToOpticianInfo"] = req
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/createProviderToOptician",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) GetProviderToOpticianList(req types.GetProviderToOpticianListReqForApi) types.GetProviderToOpticianListRespForApiResp {
	resp := types.GetProviderToOpticianListRespForApiResp{}
	body := make(map[string]any)
	body["page"] = req.Page
	body["size"] = req.Size
	if req.ProviderName != "" {
		body["providerName"] = req.ProviderName
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/getProviderToOpticianList",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) UpdateProviderToOptician(req types.ProviderToOpticianInfo) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["providerToOpticianInfo"] = req
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/updateProviderToOptician",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) DeleteProviderToOptician(req types.DeleteProviderToOpticianReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/deleteProviderToOptician",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *GoodsApi) AuthProviderToOptician(req types.ProviderToOpticianAuthInfo) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["authCode"] = req.AuthCode
	body["providerId"] = req.ProviderId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/authorizeProvider",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// getProviderToOpticianLevelList
func (c *GoodsApi) GetProviderToOpticianLevelList(req types.GetProviderToOpticianLevelListReqForApi) types.GetProviderToOpticianLevelListRespForApiResp {
	resp := types.GetProviderToOpticianLevelListRespForApiResp{}
	body := make(map[string]any)
	body["page"] = req.Page
	body["size"] = req.Size
	if req.OpticianId != 0 {
		body["opticianId"] = req.OpticianId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/provider/getProviderToOpticianLevelList",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}
